﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc.Mailer;
using RoccaStore.Mailers;
using RoccaStore.Services;
using RoccaStore.Utils;
using RoccaStore.Web.Filters;
using RoccaStore.Web.Models;

namespace RoccaStore.Web.Controllers
{
    [AccountFilter]
    public class EmailController : Controller
    {
        private readonly IUserMailer _userMailer;
        private readonly IFileInfoService _fileInfoService;
        private readonly IFileSendService _fileSendService;
        private readonly IAddressBookService _addressBookService;
        private readonly IAccountService _accountService;
        private readonly ISlugService _slugService;

        public EmailController(
            IUserMailer userMailer,
            IFileInfoService fileInfoService,
            IFileSendService fileSendService,
            IAddressBookService addressBookService,
            IAccountService accountService,
            ISlugService slugService)
        {
            _userMailer = userMailer;
            _fileInfoService = fileInfoService;
            _fileSendService = fileSendService;
            _addressBookService = addressBookService;
            _accountService = accountService;
            _slugService = slugService;
        }

        public ActionResult SendDownload(string slugName, string to, string from, FormCollection form)
        {
            bool notify = form["notify"] != null ? Convert.ToBoolean(form["notify"]) : false;

            FileInfo file = _fileInfoService.GetBySlug(slugName);
            Slug slug = _slugService.GetByName(slugName);

            if (slug != null)
            {
                IEnumerable<string> emails;

                if(to.Contains(";"))
                    emails = to.Split(';');
                else
                    emails = to.Split(',');

                List<FileSend> sends = new List<FileSend>();

                foreach (var email in emails.Distinct())
                {
                    var sendInfo = new FileSend()
                    {
                        SlugID = slug.ID,
                        From = from,
                        Notify = notify,
                        To = email
                    };
                    
                    _fileSendService.Save(sendInfo);

                    if(_accountService.Current != null)                    
                        _addressBookService.Create(sendInfo.To, _accountService.Current.ID);

                    // use existing slug data as we already have it
                    sendInfo.Slug = slug;

                    sends.Add(sendInfo);

                    _userMailer.Download(sendInfo).Send();
                }

                var model = new EmailViewModel()
                {
                    Slug = slug,
                    FileSend = sends,
                    From = from
                };

                return View("Success", model);
            }
            else
                return RedirectToAction("NotFound", "Errors");
        }
    }
}
